<?php

namespace app\web\service;

use app\common\model\trArticle;
use app\common\model\trArticleType;
use app\common\util\ErrorCode;

/**
 * Class Auth
 * @package app\admin\service
 * 文章和文章类型服务类
 */
class ArticleService
{
    private $params;
    private $returnData;
    private $mall_id = 0;

    public function __construct($data, $params)
    {
        $this->returnData = $data;
        $this->params = $params;
        $this->mall_id = request()->mallData["id"];
    }

    // 获取客服文章首页的数据
    public function getHomeData(){
        $model = new trArticleType();
        $typeData = $model->getList("id,name,icon", ["status" => 1, "mall_id" => $this->mall_id], 0, 0, "order_by desc", false);
        if(!empty($typeData)){
            $articleModel = new trArticle();
            foreach ($typeData as $k => $item){
                $typeData[$k]["data"] = $articleModel->getList("id,title", ["type_id" => $item["id"], "status" => 1], 0, 2, "create_time desc", false);
            }
        }
        $this->returnData["data"] = $typeData;
        return $this->returnData;
    }

    // 文章类型获取所有文章
    public function getArticleByTypeId(){
        if (empty($this->params['id'])) {
            return ErrorCode::code("params_not_exist", "id");
        }
        $model = new trArticle();
        $data = $model->getList("id,title", ["status" => 1, "type_id" => $this->params["id"], "mall_id" => $this->mall_id], 0, 0, "create_time desc", false);
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    // 获取文章内容
    public function getArticleContentById(){
        if (empty($this->params['id'])) {
            return ErrorCode::code("params_not_exist", "id");
        }
        $model = new trArticle();
        $data = $model->getFields("title,content,status", ["id" => $this->params["id"]]);
        if(!empty($data)) {
            if ($data["status"] != '1') {
                return ErrorCode::code("customized", "该文章不可查看");
            }
            unset($data["status"]);
        }
        $this->returnData["data"] = $data;
        return $this->returnData;
    }
}